﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogDeviceType : Form
    {
        private TextBox _retTextType = null;
        private Device_Type _typeW = null;
        private TextBox _retTextProvince = null;

        public DialogDeviceType(TextBox textType, TextBox textProvince = null, Device_Type typeW=null)
        {
            if (textType == null)
            {
                MessageBox.Show("传入参数为空");
                Close();
            }
            
            _retTextType = textType;
            _typeW = typeW;

            if (textProvince != null) _retTextProvince = textProvince;

            InitializeComponent();
        }

        //设置listview
        private void SetListView(ListView view, Device_Type type)
        {
            if (view == null || type == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = type.DeviceType;
            item.SubItems.Add(type.PROVINCE);
            item.SubItems.Add(type.SWVersion);
            item.SubItems.Add(type.HWVersion);
            item.SubItems.Add(type.BuildTime);
            item.SubItems.Add(type.XPONMode);

            view.Items.Add(item);
        }
        //清空list view
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }

        private void DialogDeviceType_Load(object sender, EventArgs e)
        {
            if (_typeW == null) _typeW = new Device_Type();

            var selectType = MethodWcf.SelectDeviceTypeClient(_typeW);
            if (selectType.result)
            {
                for (int i = 0; i < selectType.value.Count; i++)
                {
                    SetListView(this.listViewDeviceType, selectType.value[i]);
                }
            }
            else//未能获得信息则关闭这个界面
            {
                MessageBox.Show(selectType.message);
                this.Close();
            }
        }

        private void listViewDeviceType_MouseClick(object sender, MouseEventArgs e)
        {
            var choiceDeviceType = this.listViewDeviceType.SelectedItems[0].Text;//选择的ID

            _retTextType.Text = choiceDeviceType;

            if (_retTextProvince != null)
                _retTextProvince.Text = this.listViewDeviceType.SelectedItems[0].SubItems[1].Text;

            this.Close();
        }
    }
}
